<?php

class m120503_072354_create_translation extends CDbMigration {

	public function safeUp() {
		$translation = array(
			'id'=>'pk',
			'languageId'=>'integer',
			'dialect'=>'string',
			'top'=>'float',
			'left'=>'float',
			'width'=>'float',
			'height'=>'float',
			'value'=>'text',
			'userId'=>'integer',
			'name'=>'string',
			'createdAt'=>'datetime',
			'updatedAt'=>'datetime',
		);
		$this->createTable('translations', $translation);
		$this->createIndex(
			'index_translation_language_id', 'translations', 'languageId'
		);
		$this->createIndex(
			'index_translation_dialect', 'translations', 'dialect'
		);
		$this->createIndex(
			'index_translation_updated_at', 'translations', 'updatedAt'
		);

		$auth = Yii::app()->authManager;

		$auth->createOperation('translationUpdate', 'Update Translation');
		$auth->createOperation('translationDelete', 'Delete Translation');
		$auth->createOperation('translationManage', 'Manage Translation');

		$role = $auth->createRole(
			'translationModerator', 'Translation Moderator'
		);
		$role->addChild('translationUpdate');
		$role->addChild('translationDelete');
		$role->addChild('translationManage');

		$auth->addItemChild('admin', 'translationModerator');
	}

	public function safeDown() {
		$this->dropTable('translations');

		$auth = Yii::app()->authManager;

		$auth->removeItemChild('admin', 'translationModerator');

		$auth->removeItemChild('translationModerator', 'translationUpdate');
		$auth->removeItemChild('translationModerator', 'translationDelete');
		$auth->removeItemChild('translationModerator', 'translationManage');

		$auth->removeAuthItem('translationUpdate');
		$auth->removeAuthItem('translationDelete');
		$auth->removeAuthItem('translationManage');

		$auth->removeAuthItem('translationModerator');
	}

}